package com.example.administrator.shzs.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.utils.ToastUtils;
import com.example.administrator.shzs.activity.BaseActivity;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.url.Contents;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 新加地址界面
 */
public class NewAddAddressActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.iv_back_new_add_adderss_activi)
    ImageView ivBack;//f返回键
    @Bind(R.id.tv_save_address)
    TextView tvSaveAddress;//保存地址
    @Bind(R.id.et_shipping_name)
    EditText etShippingName;//收货人名
    @Bind(R.id.et_shipping_phone)
    EditText etShippingPhone;//收货人电话
    @Bind(R.id.et_detail_address)
    EditText etDetailAddress;//详细地址



    private double latitude;
    private double longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_add_address);
        ButterKnife.bind(this);
        MyApplication.app.activityList.add(this);

        setLinsteners();
    }


    public double getLatitude(){
        return  latitude;
    }

    public double getLongitude(){
        return longitude;
    }

    private void setLinsteners() {

        tvSaveAddress.setOnClickListener(this);
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        latitude= MyApplication.app.getLatitude();
        longitude=MyApplication.app.getLongitude();

        String name=etShippingName.getText().toString().trim();
        String phone=etShippingPhone.getText().toString().trim();
        String detailAddress=etDetailAddress.getText().toString().trim();
        String userId=MyApplication.app.getUserId();

        switch (v.getId()){

            case R.id.iv_back_new_add_adderss_activi://返回键

                finish();
                break;

            case R.id.tv_save_address://保存地址

                if(!name.equals("") || !phone.equals("") || !detailAddress.equals("")) {

                    //正则表达式//
                    Pattern p = Pattern.compile("^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$");
                    //判断用户的手机号码格式是否正确
                    Matcher m = p.matcher(phone);
                    boolean result = m.matches();
                    if(result) {
                        //保存地址
                        saveAddress(name, detailAddress, phone, userId, longitude, latitude);
                    }else {
                        ToastUtils.showMessageShort(this,"请输入正确的手机号码!");
                    }

                }else if(name.equals("")){
                    ToastUtils.showMessageShort(this,"请输入收货人姓名");
                }else if(phone.equals("")){
                    ToastUtils.showMessageShort(this,"请输入收货人电话号码");
                }else if(detailAddress.equals("")){
                    ToastUtils.showMessageShort(this,"请输入收货人详细地址");
                }

                break;
        }
    }

    /**
     * 保存地址
     * @param name
     * @param address
     * @param phone
     * @param userId
     * @param longitude
     * @param latitude
     */
    private void saveAddress(String name, String address, String phone, String userId, double longitude, double latitude) {

        String url= Contents.USER_NEW_ADD_USER_SHIPPING_ADDRESS;

        RequestParams params=new RequestParams(url);
        params.addBodyParameter("receiver",name);
        params.addBodyParameter("address",address);
        params.addBodyParameter("mobilephone",phone);
        params.addBodyParameter("userid",userId);
        params.addBodyParameter("dataid","1");
        params.addBodyParameter("lng",longitude+"");
        params.addBodyParameter("lat",latitude+"");
        params.addBodyParameter("op","1");

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {


                Intent intent=getIntent();
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

}
